#!/bin/bash

driver_name="my_char"
num_devices=2


function usage {
echo "$0 <driver-name> <num-devices>"
exit 1
}

if [ ! -z "$1" ]; then
    driver_name=$1
else
    usage
fi

if [ ! -z "$2" ]; then
    num_devices=$2
else 
    usage
fi

# Check if driver already installed
already=`cat /proc/devices | grep $driver_name`
if [ ! -z "$already" ]; then
   echo "Driver already installed. Clean first!!"
   exit 1
fi



echo "Inserting driver"
sudo insmod char.ko driver_name=$driver_name num_devices=$num_devices

major=`cat /proc/devices | grep $driver_name | cut -f1 -d' '`
echo "Found device major: $major";

echo "Removing stale nodes"
sudo rm -rf /dev/${driver_name}*

echo "Creating $num_devices devices for $driver_name"
count=0
while [ $count -lt $num_devices ]; do
    sudo mknod /dev/${driver_name}${count} c $major $count
    sudo chmod 777 /dev/${driver_name}${count}
    let count=$count+1
done

echo "Done creating nodes in /dev";


